//
//  KwBigImgView.m
//  KuWoCan
//
//  Created by hwd on 17/3/17.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "KwBigImgView.h"

@interface KwBigImgView ()<UIScrollViewDelegate>

@property (nonatomic,strong) UILabel *titleLab;

@property (nonatomic,strong) UIImageView *imgView;
@property (nonatomic, strong) UIScrollView *scrollView;

@end

@implementation KwBigImgView

+ (instancetype)shareInstance
{
    static dispatch_once_t onceToken;
    static KwBigImgView *instance = nil;
    dispatch_once(&onceToken,^{
        instance = [[KwBigImgView alloc] init];
    });
    return instance;
    
}

- (instancetype)init
{
    if (self = [super init])
    {
        [self setUpUI];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self centerScrollViewContents];
}

- (void)centerScrollViewContents
{
    CGSize boundsSize = self.scrollView.bounds.size; //滑块大小
    CGRect contentsFrame = _imgView.frame; //内容大小

    if (contentsFrame.size.width < boundsSize.width) { //内容小于外宽
        contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f; //宽度差除以2,取得一半,因此是内容的上点坐标
    } else {
        contentsFrame.origin.x = 0.0f; //否则挪到0点去,遮盖,或者刚好一样
    }

    //同样对高来进行处理,如果只是一边更长,那么另一边就会被约束.
    if (contentsFrame.size.height < boundsSize.height) {
        contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
    } else {
        contentsFrame.origin.y = 0.0f;
    }

    //调整框架
    _imgView.frame = contentsFrame;
}

- (void)setUpUI
{
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8 ];

    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREENW, SCREENH)];
    self.scrollView.contentSize = CGSizeMake(SCREENW, SCREENH);
    self.scrollView.maximumZoomScale = 5;
    self.scrollView.minimumZoomScale = 1;
    self.scrollView.delegate = self;
    [self addSubview:self.scrollView];

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(SCREENW - 15 - 35, 15, 35, 35);
    [btn setImage:kImg(@"close") forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(closeBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn];
//    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(@15);
//        make.right.equalTo(@-15);
//        make.size.mas_equalTo(CGSizeMake(35, 35));
//    }];
    
    _imgView = [UIImageView new];
    _imgView.image = kImg(@"BG");
    _imgView.frame = CGRectMake(0, 0, SCREENW, SCREENH);
    [self.scrollView addSubview:_imgView];
//    [_imgView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.equalTo(@0);
//        make.height.equalTo(@250);
//        make.top.equalTo(btn.mas_bottom).offset(100);
//    }];

    _titleLab = [UILabel new];
    _titleLab.backgroundColor = [UIColor clearColor];
    _titleLab.numberOfLines = 0;
    _titleLab.textAlignment = NSTextAlignmentLeft;
    _titleLab.font = KwFont(15);
    _titleLab.text = @"酷我乳业:有一种文化............";
    _titleLab.textColor = [UIColor whiteColor];
    [self addSubview:_titleLab];
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(_imgView.mas_bottom).offset(15);
        make.left.equalTo(@15);
        make.right.equalTo(@-15);
        make.bottom.equalTo(@-20);
    }];
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return _imgView;
}

- (void)closeBtnAction
{
    [self removeFromSuperview];
}


- (void)showBigImageWithTitle:(NSString *)title imageStr:(NSString *)imgStr
{
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    _titleLab.text = title;
    [_imgView sd_setImageWithURL:[NSURL URLWithString:imgStr] placeholderImage:kImg(kPlacehodelImg)];
    _imgView.contentMode = UIViewContentModeScaleAspectFit;
    [[[UIApplication sharedApplication] keyWindow] addSubview:self];
}

- (void)showBigImageWithImageStr:(id)img
{
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    if ([img isKindOfClass:[UIImage class]]) {
        _imgView.image = img;
    }else
    {
        [_imgView sd_setImageWithURL:img];
    }
    [[[UIApplication sharedApplication] keyWindow] addSubview:self];
}


@end
